//
//  LBTabBarController.m
//  loveBeen
//
//  Created by 杨烁 on 2016/11/19.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBTabBarController.h"
#import "LBNavigationController.h"
#import "LBPlusMinusTool.h"
#import "LBSuperMarketProducts.h"



@interface LBTabBarController ()<LBPlusMinusToolDelegate>

@end

@implementation LBTabBarController{
    
    NSMutableDictionary *_adDic;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _adDic = [NSMutableDictionary dictionary];
    
    [LBPlusMinusTool sharedPlusMinusTool].delegate = self;
    [self setupUI];
    
}



-(void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UINavigationController *homeController = [self controllerWithClassName:@"LBHomeController" title:@"首页" imageName:@"v2_home"];
    UIViewController *SuperMarket = [self controllerWithClassName:@"LBSuperMarketViewController" title:@"闪送超市" imageName:@"v2_order"];
    UIViewController *BuyCar= [self controllerWithClassName:@"LBBuyCarController" title:@"购物车" imageName:@"v2_shopCart"];
    UIViewController *Me= [self controllerWithClassName:@"LBMeController" title:@"我的" imageName:@"v2_my"];
    
    
    self.viewControllers = @[homeController,SuperMarket,BuyCar,Me];
    
    
    
    
}
-(void)ChangeTabbarCount:(LBPlusMinusTool *)MinusTools{
    
    NSArray *array = MinusTools.selectedProductsModel;
    
    self.viewControllers[2].tabBarItem.badgeValue = [MinusTools allCount];
    
    if(array.count ==0)
        self.viewControllers[2].tabBarItem.badgeValue = nil;
}


//控制器添加
- (UINavigationController *)controllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName {
    //1.根据类名生成一个类
    Class clz = NSClassFromString(className);
    
    //2.使用类创建一个对象
    UIViewController *controller  = [[clz alloc]init];
    
    //3.设置控制器的属性
    controller.title = title;
    
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //在创建好的控制器的外面再嵌套一个navigationController
    LBNavigationController *navController = [[LBNavigationController alloc]initWithRootViewController:controller];
    //4.返回控制器
    return navController;
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectIndex = [tabBar.items indexOfObject:item];
    [self animationWithIndex:selectIndex];
}

//item动画效果----(**)
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    
    for (UIView *tabBarButton in self.tabBar.subviews)
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")])
            [tabbarbuttonArray addObject:tabBarButton];
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount = 1;
    pulse.autoreverses = YES;
    pulse.fromValue = [NSNumber numberWithFloat:0.8];
    pulse.toValue = [NSNumber numberWithFloat:1.1];
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
    
    
}


@end
